<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_model extends MY_Model {
    /**
     * construct
     */
    public function __construct(){
        parent::__construct();

        $this->short_table = 'user';
        $this->full_table = 'pro_user';
        $this->primary_key = 'id';
    }

    /**
     * Register check user unique
     * @return mixed
     * @author clark
     */
	public function checkUniqueUser($username, $phone){
		$data = $this->db->select('user_name,phone')
                     ->where('user_name',$username)
                     ->or_where('phone',$phone)->from('user')->get()->result_array();
		return $data;
	}

    /**
     * get page
     * @param mixed $data
     *
     */
	public function pageList($data){

		$pageindex = intval($data["pageindex"]);
		$pagesize = intval($data["pagesize"]);
		$user_name = empty($data["user_name"]) == true ? "" : $data["user_name"];
        $phone = empty($data["phone"]) == true ? "" : $data["phone"];
        $parent_user_name = empty($data["parent_user_name"]) == true ? "" : $data["parent_user_name"];

	    $sql = "";
		if(empty($user_name) == false){
			$sql .= " user_name like '%$user_name%' and ";
		}
        if(empty($phone) == false){
			$sql .= " phone like '%$phone%' and ";
		}
        if(empty($parent_user_name) == false){
			$sql .= " parent_user_name like '%$parent_user_name%' and ";
		}
		$sql .= " 1 = 1";

		$start = ($pageindex - 1) * $pagesize;
		$records = $this->db->query("select * from pro_user where $sql order by id asc limit $start,$pagesize")->result_array();
		$count = $this->db->query("select count(*) as count from pro_user where $sql")->result_array();
		$count = $count[0]["count"];
		$totalPages = ceil( intval($count) / $pagesize );

		return array("code" => "success", "records" => $records, "count" => $count, "totalPages" => $totalPages);
	}
}
